using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using BO;

namespace AnimalFarm.LivestockMedication_manage
{
    public partial class AddLivestockMedication : Telerik.WinControls.UI.RadForm
    {
        B_Quarantine B_quarantine = new B_Quarantine();
        B_Livestock B_livestock = new B_Livestock();
        B_Vet B_vet = new B_Vet();
        B_Disease B_disease = new B_Disease();
        B_LivestockMedication B_livestockMedication = new B_LivestockMedication();
        public AddLivestockMedication()
        {
            InitializeComponent();
        }

        private void AddLivestockMedication_Load(object sender, EventArgs e)
        {
            loadQuarantine();
            loadVet();
            loadDisease();
        }

        //load quarantine
        private void loadQuarantine()
        {
            DataTable aTable = B_quarantine.GetAllQuarantine();
            grvQuarantined.DataSource = aTable;
        }

        //load livestock list
        private void loadLivestock(int livestockID)
        {
            DataTable aTable = B_livestock.GetAllLivestock();
            DataTable newTable = aTable.Copy();
            newTable.Clear();
            for (int i = 0; i < aTable.Rows.Count; i++)
            {
                int livestock=int.Parse(aTable.Rows[i]["LivestockID"].ToString());
                if (livestock == livestockID)
                {
                    newTable.Rows.Add(aTable.Rows[i].ItemArray);
                }
            }
            grvLivestock.DataSource = newTable;
        }

        //load vet
        private void loadVet()
        {
            DataTable aTable = B_vet.GetAllVet();
            drdVet.DataSource = aTable;
            drdVet.DisplayMember = "VetName";
            drdVet.ValueMember = "VetID";
            drdVet.Text = "(Select a vet)";
        }
        //load disease
        private void loadDisease()
        {
            DataTable aTable = B_disease.GetAllDisease();
            drdDisease.DataSource = aTable;
            drdDisease.DisplayMember = "DiseaseName";
            drdDisease.ValueMember = "DiseaseID";
            drdDisease.Text = "(Select a disease)";
        }


        private void grvLivestock_Click(object sender, EventArgs e)
        {

        }

        private void grvQuarantined_Click(object sender, EventArgs e)
        {
            int livestockID = int.Parse(grvQuarantined.SelectedRows[0].Cells["LivestockID"].Value.ToString());
            loadLivestock(livestockID);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnAddLivestockMedication_Click(object sender, EventArgs e)
        {
            int vetID = int.Parse(drdVet.SelectedValue.ToString());
            int diseaseID = int.Parse(drdDisease.SelectedValue.ToString());
            DateTime infectedDate = dtpInfectedTime.Value;
            string symptom = txtSymptom.Text;
            string treatmentMethod = txtTreatmentMethod.Text;
            DateTime treatmentDate = dtpTreatTime.Value;
            bool status = true;
            int quarantineID = int.Parse(grvQuarantined.SelectedRows[0].Cells["QuarantineID"].Value.ToString());

            LivestockMedication_Info info = new LivestockMedication_Info();
            info.DiseaseID = diseaseID;
            info.InfectedDate = infectedDate;
            info.QuarantineID = quarantineID;
            info.Symptom = symptom;
            info.TreatmentDate = treatmentDate;
            info.TreatmentMethod = treatmentMethod;
            info.TreatmentStatus = status;
            info.VetID = vetID;

            bool isOk = B_livestockMedication.CreateOneLivestockMedication(info);
            if (isOk)
            {
                MessageBox.Show(this, "Livestock medication added successful", "Livestock medication adding", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(this, "Livestock medication adding is fail", "Livestock medication adding", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
